<?php
declare (strict_types = 1);

namespace app\model;

use Carbon\Carbon;
use think\Model;
use think\Request;

/**
 * @property string $ip
 * @property integer $lp_type
 * @property string $cn_at
 * @property string $us_at
 * @property integer $is_deduplicated
 * @property integer $shop_id
 */
class IpLogs extends Model
{
    public $autoWriteTimestamp = false;
    protected $field = [
        'ip','lp_type','cn_at','us_at','is_deduplicated','shop_id'
    ];

    const LP_TYPE_VISITOR = 1;//访客
    const LP_TYPE_BUY_NOW = 2;//访客
    const LP_TYPE_CHECKOUT = 3;//访客
    const LP_TYPE_CUSTOMER = 4;//访客
    const LP_TYPE_LOGISTIC = 5;//访客
    const LP_TYPE_PAYMENT = 6;//访客
    const LP_TYPE_COMPLETE_PAYMENT = 7;//访客

    public static function saveIpLogs(Request $request,$lp_type=0)
    {
        return self::create([
            'ip'=>getClientIP(),//获取用户IP地址
            'lp_type'=>$lp_type ?:$request->post('lp_type'),
            'cn_at'=>Carbon::now()->toDateTimeString(),
            'us_at'=>Carbon::now()->addHours(-16)->toDateTimeString(),
            'shop_id'=>$request->middleware('x_shop_id'),
        ]);
    }

}
